package edu.ccut.saturn.studio.naviagtor.wizard.model;

import java.io.File;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;

public class SaturnFloderWizardPage extends SaturnWizardPage {

	public static final String SATURN_FLODER_DESCRIPTION = "description"; //$NON-NLS-1$
	public static final String SATURN_FLODER_NAME = "name"; //$NON-NLS-1$
	public static final String SATURN_FLODER_FILE_NAME = "floder"; //$NON-NLS-1$
	public static final String SATURN_FLODER_VERSION = "version"; //$NON-NLS-1$
	public static final String SATURN_FLODER_TIME = "time"; //$NON-NLS-1$
	public static final String SATURN_FLODER_TYPE = "type"; //$NON-NLS-1$

	private Text versionTxt;
	private Text descriptionText;
	private Text folderNameText;
	private Text nameText;
	private Combo combo;

	private String type;

	public SaturnFloderWizardPage(String type) {
		super("wizardPage"); //$NON-NLS-1$
		setTitle(Messages.getString("CreateActionWizardPage.saturnfilewizard")); //$NON-NLS-1$
		setDescription(Messages
				.getString("CreateActionWizardPage.dependontypecreatesaturnfile")); //$NON-NLS-1$
		this.type = type;

	}

	@Override
	public void initalValues(Map<String, Object> values) {
		values.put(SATURN_FLODER_FILE_NAME, folderNameText.getText().trim());
		values.put(SATURN_FLODER_NAME, nameText.getText().trim());
		values.put(SATURN_FLODER_DESCRIPTION, descriptionText.getText().trim());
		values.put(SATURN_FLODER_VERSION, versionTxt.getText().trim());
		values.put(SATURN_FLODER_TYPE, type);
		values.put(SATURN_FLODER_TIME, System.currentTimeMillis() + ""); //$NON-NLS-1$
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);
		initializeDialogUnits(parent);
		setControl(container);

		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setText(Messages.getString("CreateActionWizardPage.type")); //$NON-NLS-1$

		combo = new Combo(container, SWT.READ_ONLY);
		combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		combo.setItems(SaturnModelTypes.getTypes());
		combo.setText(this.type);
		combo.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(final SelectionEvent e) {
				folderNameText.setText(combo.getText());
				nameText.setText(SaturnModelTypes.getTypeDefaultFloderName(combo
						.getText()));
				type = combo.getText();
			}
		});

		final Label label = new Label(container, SWT.NONE);
		label.setText(Messages.getString("SaturnFloderWizardPage.filename")); //$NON-NLS-1$

		folderNameText = new Text(container, SWT.BORDER);
		folderNameText.setTextLimit(50);
		folderNameText.setText(combo.getText());

		final GridData gd_folderName = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		folderNameText.setLayoutData(gd_folderName);
		folderNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		final Label label2 = new Label(container, SWT.NONE);
		label2.setText(Messages.getString("SaturnFloderWizardPage.name")); //$NON-NLS-1$

		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		nameText.setText(SaturnModelTypes.getTypeDefaultFloderName(combo
				.getText()));

		final GridData gd_folderName1 = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		nameText.setLayoutData(gd_folderName1);

		nameText.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		final Label versionLabel = new Label(container, SWT.NONE);
		versionLabel.setText(Messages
				.getString("CreateActionWizardPage.editon")); //$NON-NLS-1$

		versionTxt = new Text(container, SWT.BORDER);
		versionTxt.setTextLimit(50);
		versionTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});
		versionTxt.setText("1.0"); //$NON-NLS-1$
		final GridData gd_versionTxt = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		versionTxt.setLayoutData(gd_versionTxt);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
		label_1
				.setText(Messages
						.getString("SaturnFloderWizardPage.description")); //$NON-NLS-1$

		descriptionText = new Text(container, SWT.V_SCROLL | SWT.BORDER
				| SWT.H_SCROLL);
		descriptionText.setTextLimit(200);
		final GridData gd_decriptionText = new GridData(SWT.FILL, SWT.FILL,
				true, true);
		gd_decriptionText.heightHint = 102;
		descriptionText.setLayoutData(gd_decriptionText);
		validate();
	}

	public boolean validate() {
		String folder = this.folderNameText.getText().trim();
		String foldern = nameText.getText().trim();
		String version = versionTxt.getText().trim();

		File file = new File(super.getFullPath() + File.separator + folder);

		setPageComplete(false);

		if (SaturnValidation.isEmpty(folder)) {
			setErrorMessage(Messages.getString("SaturnFloderWizardPage.IsEmpty")); //$NON-NLS-1$
			return false;
		}

		if (file.exists()) {
			setErrorMessage(Messages.getString("SaturnFloderWizardPage.IsExist")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isHasSpace(folder)) {
			setErrorMessage(Messages
					.getString("SaturnFloderWizardPage.HasBlank")); //$NON-NLS-1$
			return false;
		}
		if (SaturnValidation.isBadWord(folder)) {
			setErrorMessage(Messages
					.getString("SaturnFloderWizardPage.HasBadword")); //$NON-NLS-1$
			return false;
		}
		if (SaturnValidation.isPriNum(folder)) {
			setErrorMessage(Messages
					.getString("SaturnFloderWizardPage.StartByNumber")); //$NON-NLS-1$
			return false;
		}
		if (!SaturnValidation.isValidProgramStr(folder)) {
			setErrorMessage(Messages
					.getString("SaturnFloderWizardPage.NumberWordUnderline")); //$NON-NLS-1$
			return false;
		}
		if (SaturnValidation.isBadWord(foldern)) {
			setErrorMessage(Messages
					.getString("SaturnFloderWizardPage.NameHasBadword")); //$NON-NLS-1$
			return false;
		}
		if (SaturnValidation.isEmpty(version)) {
			setErrorMessage(Messages
					.getString("SaturnFloderWizardPage.VesionIsEmpty")); //$NON-NLS-1$
			return false;
		}
		if (SaturnValidation.isBadWord(version)) {
			setErrorMessage(Messages
					.getString("SaturnFloderWizardPage.IsBadword")); //$NON-NLS-1$
			return false;
		} else {
			setErrorMessage(null);
			setPageComplete(true);

			return true;
		}

	}
}
